/**
 * 员工管理（添加|修改）employeeOption.js
 * 版本号： v1.0.0
 */
//项目完结选择初始化值
var prEndStateSelFlag = "1";
$(function(){

    //设置输入框的最大输入长度
    $("#name").textbox("textbox").attr("maxlength", 12);
    $("#jobNumber").textbox("textbox").attr("maxlength", 18);
    $("#employeePhone").textbox("textbox").attr("maxlength", 11);
    $("#idCard").textbox("textbox").attr("maxlength", 18);
    $("#regAddress").textbox("textbox").attr("maxlength", 26);
    $("#thrClsCertNum").textbox("textbox").attr("maxlength", 20);
    $("#email").textbox("textbox").attr("maxlength", 40);
    $("#remark").textbox("textbox").attr("maxlength", 202);

    //所属公司下拉树
    $.ajax({
        url: CONTEXT_PATH + "/company/getTreeCompany.do",
        type: "get",
        success: function (data) {
            var isAdd = $("#emId").val();
            // var themecombo = "";
            // themecombo += data.substring(1);
            var  themecombo = JSON.parse(data);
            $("#companyTree").combotree("loadData",themecombo);
            $("#companyTree").combotree({
                onChange:function(node){
                    $('#companyId').val(node);
                    //在选择后根据选择公司的ID去查询部门下拉框
                    var companyId = $('#companyId').val();
                    $("#officeTree").combotree("loadData",{});
                    $.ajax({
                        url: CONTEXT_PATH + "/office/getTree.do",
                        data:{
                            "companyId":companyId
                        },
                        type: "get",
                        success: function (data) {
                            var themecombo = JSON.parse(data);
                            $("#officeTree").combotree("loadData",themecombo);
                            $("#officeTree").combotree({
                                onChange:function(node){
                                    $('#officeId').val(node);
                                    var officeId = node;
                                    //在选择后根据选择公司的ID去查询项目下拉框
                                    $("#projectList").combobox("loadData",{});
                                    $.ajax({
                                        url: CONTEXT_PATH + "/project/projectList.do",
                                        data:{
                                            "companyId":companyId,
                                            "officeId":officeId
                                        },
                                        type: "get",
                                        success: function (data) {
                                            var themecombo = "";
                                            if(data.length > 2){
                                                themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
                                                themecombo += data.substring(1);
                                            }else{
                                                themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"}]";
                                            }
                                            themecombo = JSON.parse(themecombo);
                                            $("#projectList").combobox("loadData",themecombo);
                                            $("#projectList").combobox({
                                                valueField:'id',
                                                textField:'text',
                                                onSelect:function(node){
                                                    $('#prId').val(node.id);
                                                    if(prEndStateSelFlag != "1"){
                                                        $('#prEndState').combobox("setValue","1");
                                                    }
                                                }
                                            });
                                            if($('#prId').val() == null || $('#prId').val() == ""){
                                                $("#projectList").combobox("setValue","");
                                            }else{
                                                $("#projectList").combobox("setValue",$('#prId').val());
                                            }
                                        }
                                    });
                                }
                            });
                            if(!isAdd || isAdd == ""){
                                $("#officeTree").combotree("setValue","");
                            }else{
                                var officeId = $("#officeId").val();
                                var oldOfficeId = $("#oldOfficeId").val();
                                var companyId = $("#companyId").val();
                                var oldCompanyId = $("#oldCompanyId").val();
                                if(oldOfficeId == officeId && oldCompanyId == companyId){
                                    $("#officeTree").combotree("setValue",officeId);
                                }
                            }
                        }
                    });

                    //在选择后根据选择公司的ID去查询项目下拉框
                    $("#projectList").combobox("loadData",{});
                    companyId = $('#companyId').val();
                    $.ajax({
                        url: CONTEXT_PATH + "/project/projectList.do",
                        data:{
                            "companyId":companyId
                        },
                        type: "get",
                        success: function (data) {
                            var themecombo = "";
                            if(data.length > 2){
                                themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
                                themecombo += data.substring(1);
                            }else{
                                themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"}]";
                            }
                            themecombo = JSON.parse(themecombo);
                            $("#projectList").combobox("loadData",themecombo);
                            $("#projectList").combobox({
                                valueField:'id',
                                textField:'text',
                                onSelect:function(node){
                                    $('#prId').val(node.id);
                                    if(prEndStateSelFlag != "1"){
                                        $('#prEndState').combobox("setValue","1");
                                    }
                                }
                            });
                            $("#projectList").combobox("setValue","");
                        }
                    });
                }
            });
            $("#officeTree").combotree("setText","--请选择--");
            $("#projectList").combobox("setText","--请选择--");
            //修改操作时，数据加载完成后给各个选框赋值
            var emId = $("#emId").val();
            if(null!=emId && ""!=emId){
                setValue();
            }
        }
    });

    //操作按钮点击
    $('#optionEmployee').bind('click', function(){
        doOptionEmployeeMethod();
    })

    //修改操作时，数据加载完成后给各个选框赋值
    // setValue();

    $("#idCard").textbox('textbox').blur(function(){
        var val = $("#idCard").textbox('getValue');
        var falg = /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(val);
        if(falg){
            var birthday = "";
            if(val.length == 15){
                birthday = "19"+val.substr(6,6);
            } else if(val.length == 18){
                birthday = val.substr(6,8);
            }
            birthday = birthday.replace(/(.{4})(.{2})/,"$1-$2-");
            $('#birthday').datebox('setValue', birthday);
        }
    });

    //上传操作
    $('#uploadBtn').bind('click', function(){
        uploadFile('uploadBtn','uploadfile','fileName','fileId','url','size','fix','md5','showImg');
    })

    //图片回显
    var fileId = $('#fileId').val();
    if(fileId != null && fileId != ""){
        $.ajax({
            url: CONTEXT_PATH + "/commonFile/getFileUrlById.do?id=" + fileId,
            type: "get",
            success: function (data) {
                var url = data.message;
                var imgDiv = $('#showImg');
                if(url != "" && url != null){
                    imgDiv.attr('src',url);
                }
            }
        });
    }

    //文件选择改变后调整
    $('#uploadfile').filebox({
        onChange:function(n,o){
            $('#uploadBtn').linkbutton('enable');
        }
    })

    //修改时填入文件名称
    var compLogoName = $('#fileName').val();
    if(compLogoName != null && compLogoName != ""){
        $('#uploadfile').filebox({prompt:compLogoName});
    }

    //项目状态完结change
    $("#prEndState").combobox({
        onChange: function (newValue, oldValue) {
            //项目完结状态为未完结时的处理
            prEndStateSelFlag = "2";
            if(newValue == 1){
                //所属项目id
                var prId = $('#prId').val();
                //已完结项目
                var prFinished = $("#prFinished").val();
                //判断已完结项目中是否包含当前选择项目，存在给出提示
                if(prFinished.indexOf(prId) >= 0){
                    $.messager.confirm("系统提示", "选择的项目为已完结，确定更改吗？", function(r) {
                        if (!r) {
                            $("#prEndState").combobox("setValue","2");
                        }
                    });
                }
            }
        }
    })
});


/**
 * 执行保存操作方法
 */
function doOptionEmployeeMethod(){
    var params = $("#operate_form").serialize();
    $("#operate_form").form("submit",{
        cache:false,
        data: params,
        url: CONTEXT_PATH + "/employee/save.do",
        onSubmit:function() {
            var flag = $(this).form("validate");
            if(flag){
                preventSubmit(null, "optionEmployee");
            }
            return flag;
        },
        success:function(data) {
            var obj = JSON.parse(data);
            var url = CONTEXT_PATH + "/employee/index.do";
            saveJump(obj,url);
        },
    });
}

/**
 * 在修改页面的时候，获取所属公司，部门，项目的值，并给下拉框设置当前选中
 */
function setValue(){
    //获取所属公司值，并给下拉框设置当前选中
    var companyId =$('#companyId').val();
    var node ={};
    if(companyId != null && companyId != "" && companyId != "0"){
        $('#companyTree').combotree('setValue', companyId);
        node.id = companyId;
        $('#companyTree').combotree("options").onSelect(node);
    }else{
        companyId = $("#defaultCompanyId").val();
        $('#companyTree').combotree('setValue', companyId);
        node.id = companyId;
        $('#companyTree').combotree("options").onSelect(node);
    }
    //添加时，默认设置当前用户的公司
    var usId = $("#usId").val();
    if(usId == null || usId == ""){
        var defaultCompanyId = $('#defaultCompanyId').val();
        if(defaultCompanyId != null && defaultCompanyId != "" && defaultCompanyId != "0"){
            $('#companyTree').combotree('setValue', defaultCompanyId);
            companyId = defaultCompanyId;
            //加载该公司下的项目部
            $.ajax({
                url: CONTEXT_PATH + "/office/getTree.do",
                data:{
                    "companyId":companyId
                },
                type: "get",
                success: function (data) {
                     var themecombo = JSON.parse(data);
                    $("#officeTree").combotree("loadData",themecombo);
                }
            });
            //加载该公司下的项目
            // $.ajax({
            //     url: CONTEXT_PATH + "/project/projectList.do",
            //     data:{
            //         "companyId":companyId
            //     },
            //     type: "get",
            //     success: function (data) {
            //         var themecombo = "";
            //         if(data.length > 2){
            //             themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
            //             themecombo += data.substring(1);
            //         }else{
            //             themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"}]";
            //         }
            //         themecombo = JSON.parse(themecombo);
            //         $("#projectList").combobox("loadData",themecombo);
            //         $("#projectList").combobox({
            //             valueField:'id',
            //             textField:'text',
            //             onSelect:function(node){
            //                 $('#prId').val(node.id);
            //             }
            //         });
            //         $("#projectList").combobox("setValue","");
            //     }
            // });
        }
    }
    //获取所属部门值，并给下拉框设置当前选中
    var officeId = $("#officeId").val();
    if(officeId){
        $.ajax({
            url: CONTEXT_PATH + "/office/getTree.do",
            data:{
                "companyId":companyId
            },
            type: "get",
            success: function (data) {
                var themecombo = JSON.parse(data);
                $("#officeTree").combotree("loadData",themecombo);
                $("#officeTree").combotree({
                    onSelect:function(node){
                        $('#officeId').val(node.id);
                        var officeId = node.id;
                        //在选择后根据选择公司的ID去查询项目下拉框
                        // $("#projectList").combobox("clear");
                        // $.ajax({
                        //     url: CONTEXT_PATH + "/project/projectList.do",
                        //     data:{
                        //         "companyId":companyId,
                        //         "officeId":officeId
                        //     },
                        //     type: "get",
                        //     success: function (data) {
                        //         var themecombo = "";
                        //         if(data.length > 2){
                        //             themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
                        //             themecombo += data.substring(1);
                        //         }else{
                        //             themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"}]";
                        //         }
                        //         themecombo = JSON.parse(themecombo);
                        //         $("#projectList").combobox("loadData",themecombo);
                        //         $("#projectList").combobox({
                        //             valueField:'id',
                        //             textField:'text',
                        //             onSelect:function(node){
                        //                 $('#prId').val(node.id);
                        //             }
                        //         });
                        //         $("#projectList").combobox("setValue","");
                        //     }
                        // });
                    }
                });
                $("#officeTree").combotree("setValue",officeId);
                //获取所属项目的值，并给下拉框设置选中
                var projectId = $('#oldPrId').val();
                if(companyId && projectId){
                    // 在选择后根据选择公司的ID去查询项目下拉框
                    $.ajax({
                        url: CONTEXT_PATH + "/project/projectList.do",
                        data:{
                            "companyId":companyId,
                            "officeId":officeId
                        },
                        type: "get",
                        async:false,
                        success: function (data) {
                            var themecombo = "";
                            if(data.length > 2){
                                themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
                                themecombo += data.substring(1);
                            }else{
                                themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"}]";
                            }
                            themecombo = JSON.parse(themecombo);
                            $("#projectList").combobox("loadData",themecombo);
                            $("#projectList").combobox({
                                valueField:'id',
                                textField:'text',
                                onSelect:function(node){
                                    $('#prId').val(node.id);
                                    if(prEndStateSelFlag != "1"){
                                        $('#prEndState').combobox("setValue","1");
                                    }
                                }
                            });
                            $("#projectList").combobox("setValue",projectId);
                        }
                    });
                }
            }
        });
    }else{
        //获取所属项目的值，并给下拉框设置选中
        var projectId = $('#prId').val();
        if(companyId && projectId){
            //在选择后根据选择公司的ID去查询项目下拉框
            $.ajax({
                url: CONTEXT_PATH + "/project/projectList.do",
                data:{
                    "companyId":companyId
                },
                type: "get",
                success: function (data) {
                    var themecombo = "";
                    if(data.length > 2){
                        themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
                        themecombo += data.substring(1);
                    }else{
                        themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"}]";
                    }
                    themecombo = JSON.parse(themecombo);
                    $("#projectList").combobox("loadData",themecombo);
                    $("#projectList").combobox({
                        valueField:'id',
                        textField:'text',
                        onSelect:function(node){
                            $('#prId').val(node.id);
                            if(prEndStateSelFlag != "1"){
                                $('#prEndState').combobox("setValue","1");
                            }
                        }
                    });
                    $("#projectList").combobox("setValue",projectId);
                }
            });
        }
    }
}